package com.czn.controller.admin;

import com.czn.entity.Option;
import com.czn.service.IOptionService;
import com.czn.vo.ResponseResult;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * 医院科目、科室管理
 *
 * @author ChenZeNan
 * @create 2021-04-22 21:53
 */
@Controller
@RequestMapping("/admin/option")
public class AdminOptionController {

    @Autowired
    private IOptionService optionService;

    /**
     * 跳转到科目、科室管理页面
     *
     * @param pageNo
     * @param pageSize
     * @param name
     * @param type
     * @param model
     * @return
     */
    @RequestMapping("/manage")
    public String optionManage(@RequestParam(required = false, defaultValue = "1") Integer pageNo,
                               @RequestParam(required = false, defaultValue = "5") Integer pageSize,
                               @RequestParam(required = false) String name,
                               @RequestParam(required = false) String type,
                               Model model) {
        PageQuery<Option> page = optionService.findOptionList(pageNo, pageSize, name, type);
        model.addAttribute("page", page);
        model.addAttribute("pageNo", pageNo);
        model.addAttribute("name", name);
        model.addAttribute("type", type);
        model.addAttribute("path", "/admin/option/manage");
        model.addAttribute("params", "");
        if (name != null && type != null) {
            model.addAttribute("params", "&name=" + name + "&type=" + type);
        }
        return "admin/optionManage";
    }

    /**
     * 跳转到新增科室页面
     *
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String toAddFormPage(Model model) {
        model.addAttribute("option", new Option());
        return "admin/optionForm";
    }

    /**
     * 跳转到科室修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public String toEditFormPage(@PathVariable Integer id, Model model) {
        model.addAttribute("option", optionService.findOptionById(id));
        return "admin/optionForm";
    }

    /**
     * 新增科室
     *
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult add(@RequestBody Option option) {
        ResponseResult result = optionService.addOption(option);
        return result;
    }

    /**
     * 编辑科室
     *
     * @param option
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.PUT)
    @ResponseBody
    public ResponseResult edit(@RequestBody Option option) {
        ResponseResult result = optionService.editOption(option);
        return result;
    }

    /**
     * 删除科室
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResponseResult delete(@PathVariable Integer id) {
        ResponseResult result = optionService.deleteOptionById(id);
        return result;
    }


}
